package com.cn.xiaonuo.slbfinances.slbmarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.common.constant.SystemConstant;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.StringUtils;
import com.cn.xiaonuo.dbs.core.annotation.DataSource;
import com.cn.xiaonuo.slbfinances.members.entity.SlUser;
import com.cn.xiaonuo.slbfinances.members.mapper.SlUserMapper;
import com.cn.xiaonuo.slbfinances.slbmarket.entity.SlBuyRoyaltyLog;
import com.cn.xiaonuo.slbfinances.slbmarket.mapper.SlBuyRoyaltyLogMapper;
import com.cn.xiaonuo.slbfinances.slbmarket.mapper.SlbMaketMapper;
import com.cn.xiaonuo.slbfinances.slbmarket.service.ISlbMarketService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class SlbMarketServiceImpl extends ServiceImpl<SlbMaketMapper,String> implements ISlbMarketService {

    @Resource
    private SlbMaketMapper slbMaketMapper;
    @Resource
    private SlUserMapper slUserMapper;
    @Resource
    private SlBuyRoyaltyLogMapper slBuyRoyaltyLogMapper;

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage buySuperiorPaymentList(String buySuperiorId, String buySuperiorName, String buySuperiorPhone, String paymentState, String confirmBeginTime, String confirmEndTime, String orderId, Integer pageNum, Integer pageSize, String buyUserId, String royaltyCondition) {
        BusinessMessage message = new BusinessMessage();
        try {
            //0.时间转换
            if (StringUtils.isNotBlank(confirmBeginTime)) {
                confirmBeginTime = SystemConstant.SDF.format(new Date(new Long(confirmBeginTime)));
            }
            if (StringUtils.isNotBlank(confirmEndTime)) {
                confirmEndTime = SystemConstant.SDF.format(new Date(new Long(confirmEndTime)));
            }

            //1.设置分页参数
            if (null == pageNum || pageNum <= 1) {
                pageNum = 1;
            }
            if (null == pageSize || pageSize <= 1) {
                pageSize = 10;
            }
            //2.分页
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> maps = slbMaketMapper.buySuperiorPaymentList(buySuperiorId,buySuperiorName,buySuperiorPhone,paymentState,confirmBeginTime,confirmEndTime,orderId,buyUserId,royaltyCondition);
            PageInfo<Map<String, Object>> data = new PageInfo<>(maps);
            //3.返回数据
            message.setData(data);
            message.setMessage("操作成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage buySuperiorPayment(String loginUserId,String buyRoyaltyId,String paymentMoney){
        BusinessMessage message = new BusinessMessage();
        try {
            //0.验证必填参数不能为空
            if(StringUtils.isBlank(loginUserId) || StringUtils.isBlank(buyRoyaltyId) || StringUtils.isBlank(paymentMoney)){
                message.setMessage("必填参数不能为空");
                message.setSuccess(false);
                return message;
            }
            //1.获得前登录用户，验证权限
            SlUser loginUser = slUserMapper.selectById(loginUserId);
            if(loginUser == null){
                message.setMessage("当前登录用户不存在");
                message.setSuccess(false);
                return message;
            }
            //2.查询要修改的记录
            SlBuyRoyaltyLog slBuyRoyaltyLog = slBuyRoyaltyLogMapper.selectById(buyRoyaltyId);
            //3.添加结款数据
            slBuyRoyaltyLog.setRecorderId(loginUser.getId());
            slBuyRoyaltyLog.setPaymentState(1);
            slBuyRoyaltyLog.setPaymentMoney(new BigDecimal(paymentMoney).setScale(2,BigDecimal.ROUND_HALF_UP));
            slBuyRoyaltyLog.setPaymentAt(SystemConstant.SDF.format(new Date()));
            slBuyRoyaltyLogMapper.updateById(slBuyRoyaltyLog);
            //4.返回操作结果
            message.setMessage("操作成功");
            message.setSuccess(true);
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            return message;
        }
    }

    //02.订单购买者上级提成--财务结款--批量操作
    @Override
    @DataSource(name = "jyb_soole_data_hn")
    public BusinessMessage buySuperiorPaymentAll(String buyRoyaltyIdList){
        BusinessMessage message = new BusinessMessage();
        try {
            //3.去掉前端参数中的中括号，转为数组
            buyRoyaltyIdList = buyRoyaltyIdList.replace("[","");
            buyRoyaltyIdList = buyRoyaltyIdList.replace("]","");
            String[] idList = buyRoyaltyIdList.split(",");//截取为数组
            //4.遍历数据进行修改
            int i = slbMaketMapper.buySuperiorPaymentAll(97043,idList);
            //5.返回数据
            message.setSuccess(true);
            message.setMessage("成功操作"+i+"条数据");
            return message;
        } catch (Exception e) {
            e.printStackTrace();
            message.setSuccess(false);
            message.setMessage("操作失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
            return message;
        }
    }
}
